import json
from datetime import datetime

from alembic.ddl import postgresql
from sqlalchemy import Integer, Column, String, Enum, DateTime, ForeignKey, Float, Text, Boolean, PrimaryKeyConstraint, \
    text, func
from sqlalchemy.orm import relationship
import sqlalchemy as sa
from sqlalchemy.dialects.postgresql import JSONB

from src.extension.extension_db import db
from src.model import  ExerciseUser


class StudentUserAnswer(db.Model):
    """学生答题记录表模型"""
    __tablename__ = 'student_user_answers'
    __table_args__ = (
        PrimaryKeyConstraint('answer_id', name='student_answers_user_pkey'),
    )

    answer_id = Column(Integer, nullable=False, autoincrement=True)
    user_id = Column(Integer, ForeignKey('users.user_id'), nullable=False)
    exercise_id = Column(Integer, ForeignKey('exercises_users.exercise_id'), nullable=False)
    user_answer = Column(Text, nullable=False)
    category_id = Column(Integer, ForeignKey('category_users.id'), nullable=False)
    solution_idea  =  Column(String, nullable=False)
    exercises_text = Column(String , nullable=False)
    branch_id = Column(String, nullable=False)
    score = Column(Float, nullable=False)
    answer_result = Column(String, nullable=False)
    detailed_proof =  Column(JSONB,server_default=sa.text("'[]'::jsonb"), nullable=False)
    key_knowledge=Column(JSONB,server_default=sa.text("'[]'::jsonb"), nullable=False)
    is_correct = Column(Boolean, nullable=False)
    created_at = Column(DateTime, nullable=False, server_default=text('CURRENT_TIMESTAMP(0)'))
    updated_at = Column(DateTime, nullable=False, server_default=text('CURRENT_TIMESTAMP(0)'), server_onupdate=text('CURRENT_TIMESTAMP(0)'))
    def getTotalCount(self):
        return db.session.query(func.count(StudentUserAnswer.answer_id)).filter(StudentUserAnswer.user_id == self.user_id,StudentUserAnswer.branch_id==self.branch_id).scalar()

    def getErrorAnswer(self):
        return db.session.query(StudentUserAnswer).filter(StudentUserAnswer.branch_id==self.branch_id,StudentUserAnswer.user_id==self.user_id,StudentUserAnswer.is_correct==False,).all()
    @property
    def getTime(self):
        from src.model import  LearningUserProgress
        return db.session.query(func.sum(LearningUserProgress.duration)).filter(LearningUserProgress.user_id==self.user_id,LearningUserProgress.branch_id==self.branch_id).scalar()
    @property
    def getScore(self):
        return db.session.query(func.sum(StudentUserAnswer.score)).filter(StudentUserAnswer.is_correct==True).scalar()
    @property
    def getExercie_question_text(self)->str:
        data = db.session.query(ExerciseUser).filter(ExerciseUser.user_id==self.user_id,ExerciseUser.branch_id==self.branch_id,ExerciseUser.exercise_id==self.exercise_id).first()
        return data.question_text
    @property
    def get_exercise_id(self):
       return db.session.query(ExerciseUser).filter_by(exercise_id=self.exercise_id).one_or_none()
    @property
    def getCateGoryName(self):
        from src.model.category_user import CategoryUser
        c =  db.session.query(CategoryUser).filter(CategoryUser.id==self.category_id).one_or_none()
        return c.name

